﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Invoicing.Model.Products;
namespace Invoicing.Command.Products
{
    public class ProductMaterialCommand
    {
        public static ProductMaterialCollection Gets(ProductMaterialQuery sea)
        {
            var ret = new ProductMaterialCollection();
            using (DBCentre db = new DBCentre())
            {
                IQueryable<ProductMaterial> q = db.DataContext.ProductMaterials;
                if (sea != null)
                {
                    if (sea.MType > 0)
                        q = q.Where(p => p.MType == sea.MType);
                }
                var n = q.Count();
                if (n > 0)
                {
                    q = q.OrderBy(p => p.ID);
                    var lst = q.ToList();
                    ret = Convertor.ConvertCollection<ProductMaterialModel, ProductMaterial, ProductMaterialCollection>(lst);
                    ret.TotalRecordsCount = n;
                }
            }
            return ret;
        }

        public static ProductMaterialModel GetModel(int id)
        {
            using (DBCentre db = new DBCentre())
            {
                var d = db.DataContext.ProductMaterials.FirstOrDefault(p => p.ID == id);
                if (d != null)
                {
                    var ret = Convertor.ConvertTo<ProductMaterialModel, ProductMaterial>(d);
                    return ret;
                }
            }
            return null;
        }
    }
}
